import 'dart:ui';

import 'package:cat_eye/config/color_config.dart';
import 'package:cat_eye/config/string_config.dart';
import 'package:cat_eye/method_channel/method_channel_manager.dart';
import 'package:cat_eye/root_page/my_attention_page/my_attention_widget.dart';
import 'package:cat_eye/root_page/my_page/my_setting_widget.dart';
import 'package:cat_eye/util/sp_util.dart';
import 'package:cat_eye/util/util.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

/*
* @Package: home_page
* @ClassName: my_page
* @Author: szj
* @CreateDate: 7/28/21 1:22 PM
* 我的页面
*/
class MyPageWidget extends StatefulWidget {
  const MyPageWidget({Key key}) : super(key: key);

  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPageWidget> {
  File _image;

  @override
  void initState() {
    super.initState();
    //修改状态栏颜色
    statusBarColor(Colors.transparent);

    //从SP中获取 相册选取图片
    //起名字太难了,自己的项目偷个小懒
    a();
  }

  void a() async {
    print("_MyPageStateInitState0");
    String path = await SpUtil.getDate<String>(StringConfig.SP_FILE_IMAGE);
    if (path != null && path != "") {
      _image = File(path);
    }

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    print("_MyPageStateInitState2:$_image");
    return Scaffold(
      body: SizedBox(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: [
            //顶部布局
            _headImage(context),

            //设置按钮
            extractListTile(context, Icons.settings, StringConfig.SETTING,
                MyPageWidgetState.SETTING),

            //关注按钮
            extractListTile(context, Icons.follow_the_signs,
                StringConfig.MY_FOLLOW, MyPageWidgetState.FOLLOW),

            //混合开发测试
            extractListTile(context, Icons.android_outlined,
                StringConfig.NATIVE_ANDROID, MyPageWidgetState.NATIVE_ANDROID),
          ],
        ),
      ),
    );
  }

  /*
   * context 上下文
   * leading 左侧按钮
   * title 标题
   * type 类型
   */
  Widget extractListTile(
      BuildContext context, IconData leading, String title, int type) {
    return InkWell(
      onTap: () => navigator(context, type),
      child: ListTile(
        leading: Icon(leading),
        title: Text(title),
        trailing: Icon(Icons.keyboard_return),
      ),
    );
  }

  //跳转页面
  void navigator(context, type) {
    if (type == MyPageWidgetState.SETTING) {
      // Navigator.push(context, router(widget: MySettingWidget()));
      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return MySettingWidget();
      }));
    } else if (type == MyPageWidgetState.FOLLOW) {
      Navigator.push(context, router(widget: MyAttentionWidget()));
    } else if (type == MyPageWidgetState.NATIVE_ANDROID) {
      MethodChannelManager.getInstance().showToast("android当前时间是多少?");
    }
  }

  Widget _headImage(BuildContext context) {
    // print("szjHeadImage:${_image.path}");

    return InkWell(
      onTap: () => openAlbum(context),
      child: SizedBox(
        height: 240,
        child: Stack(
            //所有Widget 居中
            alignment: Alignment.center,
            children: [
              Image.asset(
                "images/launch.png",
                fit: BoxFit.cover,
                width: MediaQuery.of(context).size.width,
                // height: 200,
              ),

              //高斯模糊
              Positioned.fill(
                child: BackdropFilter(
                  filter: ImageFilter.blur(
                    sigmaX: 5,
                    sigmaY: 5,
                  ),
                  child: Container(
                    color: Colors.white10,
                  ),
                ),
              ),

              Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  //头像
                  Container(
                      width: 66,
                      height: 66,
                      padding: EdgeInsets.all(5),
                      decoration: BoxDecoration(
                          border: Border.all(
                              color: ColorConfig.themeColor, width: 2),
                          // color: Colors.white,
                          borderRadius: BorderRadius.circular(50)),
                      alignment: Alignment.center,
                      child: _image == null
                          ? Image.asset(
                              "images/flutter_android.png",
                              width: 44,
                              height: 44,
                            )
                          : Image.file(
                              _image,
                              width: 44,
                              height: 44,
                            )),

                  Padding(
                      padding: EdgeInsets.only(top: 5),
                      child: Text(
                        StringConfig.ANDROID_SUPER_SOLDIER,
                        style: TextStyle(color: ColorConfig.themeColor),
                      ))
                ],
              ),
            ]),
      ),
    );
  }

  /*
   *  这里赋值的我之前的项目
   *  可以参考我之前写的博客[https://blog.csdn.net/weixin_44819566/article/details/109511943]
   *
   *    //打开相册
   */
  void openAlbum(BuildContext context) {
    showModalBottomSheet(
        //背景色
        backgroundColor: Colors.white,
        //点击时底部面板颜色
        barrierColor: Colors.black.withAlpha(100),
        //点击时,底部面板以外的颜色
        shape: RoundedRectangleBorder(
            //设置样式
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(8), topRight: Radius.circular(8))),
        isScrollControlled: false,
        //true 全屏 false 半屏
        isDismissible: true,
        //true外部可以点击  false外部不可以点击
        elevation: 40,
        context: context,
        builder: (BuildContext context) {
          return Container(
            height: 140,
            child: ListView(
              children: [
                Column(
                  children: [
                    _item(StringConfig.PHOTO_GRAPH, false, "", context),
                    _item(StringConfig.SELECT_FROM_ALBUM, true, "", context),
                  ],
                ),
              ],
            ),
          );
        });
  }

  ///  title 主标题
  ///  bool true相册选取 false拍照选取
  ///  subTitle 副标题
  _item(String title, bool bool, String subTitle, BuildContext context) {
    return GestureDetector(
      child: ListTile(
        //背景色
        tileColor: Colors.black,
        //背景色会把圆角覆盖掉!!
        //副标题
        // subtitle:,

        // 将字体缩小
        // dense: true,

        leading: Padding(
          padding: EdgeInsets.only(left: 10),
          child: Icon(
            !bool ? Icons.photo_camera : Icons.photo_album,
            size: 25,
            color: Colors.black,
          ),
        ),
        //左边显示图片
        // trailing: Icon(Icons.android),

        //末尾显示图片
        contentPadding: EdgeInsets.all(3),
        //内边距 默认16
        title: text(
          title,
          fontColor: Colors.black,
        ),
        onTap: () => getImage(context, bool),
        selected: true,
        //如果选中,则颜色会跟随主题颜色
        enabled: true, //禁止点击事件
      ),
    );
  }

  final picker = ImagePicker();

  /*
   * ImageSource.camera 相机拍照
   * ImageSource.gallery 相册选取图片
   */
  Future getImage(BuildContext context, bool isCamera) async {
    final pickedFile = await picker.getImage(
        source: !isCamera ? ImageSource.camera : ImageSource.gallery);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);

        //保存选取的图片
        SpUtil.setData<String>(StringConfig.SP_FILE_IMAGE, _image.path);

        Navigator.pop(context);
      } else {
        print('No image selected.');
      }
    });
  }
}

class MyPageWidgetState {
  //设置
  static const int SETTING = 1;

  //关注
  static const int FOLLOW = 2;

  //native 混合开发 android
  static const int NATIVE_ANDROID = 3;
}
